package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.LoginDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserService;
import com.heima.utils.AppJwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;

@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {

    @Autowired
    private ApUserMapper apUserMapper;

    /**
     * 用户登录
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(LoginDto dto) {
        //1.判断入参是否为空
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);//dto不能为空
        }

        String phone = dto.getPhone();
        String password = dto.getPassword();
        //2.判断手机号和密码是否存在

        //4.游客登录
        if (StringUtils.isEmpty(phone) && StringUtils.isEmpty(password)) {
            String token = AppJwtUtil.getToken(0L);//设置token
            //返回结果 user:null + token
            HashMap<String, Object> map = new HashMap<>();
            map.put("user", null);
            map.put("token", token);
            return ResponseResult.okResult(map);
        }

        //3.正常登录
        if (StringUtils.isNotEmpty(phone) && StringUtils.isNotEmpty(password)) {
            LambdaQueryWrapper<ApUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApUser::getPhone, phone);
            ApUser user = apUserMapper.selectOne(queryWrapper);
            if (user == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);//用户不存在
            }
            //手机号存在,则进行密码比对
            String passwordFormDataBase = user.getPassword();//后台用户存储的密码(md5加密)
            String salt = user.getSalt();//加密盐
            String passwordAfterMd5 = DigestUtils.md5DigestAsHex((password.concat(salt)).getBytes());
            if (!passwordAfterMd5.equals(passwordFormDataBase)) {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);//登陆失败
            }
            //生成token,把token和用户信息返回给前端
            String token = AppJwtUtil.getToken(user.getId().longValue());//设置token
            //返回结果 user + token
            HashMap<String, Object> map = new HashMap<>();
            user.setPassword(null);//清空敏感数据
            user.setSalt(null);//清空敏感数据
            map.put("user", user);
            map.put("token", token);
            return ResponseResult.okResult(map);//登录成功
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);//dto内的phone和password不能为空
    }
}